package youleche.com.business;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

import youleche.com.connection.ConnectionFactory;
import youleche.com.connection.JdbcHelper;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.IShare;
import youleche.com.model.MShare;

public class BShare {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 处理器
	 */
	private IShare handler = null;
	
	/**
	 * 构造器
	 */
	public BShare(){
		this.handler = DaoFactory.getDShare();
	}
	
	/**
	 * 添加分享攻略
	 * @param share 攻略信息
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int addShare(MShare share) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.addShare(this.con, share);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取分享信息列表
	 * @param startIndex 开始索引
	 * @param endIndex 每页数量
	 * @param orderBy 排序方式
	 * @return 返回信息列表
	 * @throws Exception 异常
	 */
	public List<MShare> getShareList(String startIndex,
			String endIndex, String orderBy) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getShareList(con, startIndex, endIndex, orderBy);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据id获取骑游攻略
	 * @param id id
	 * @return 返回骑游攻略
	 * @throws Exception 异常
	 */
	public MShare getShareById(String id) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getShareById(this.con, id);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取分享信息列表
	 * @param startIndex 开始索引
	 * @param endIndex 每页数量
	 * @param orderBy 排序方式
	 * @param userName  用户名
	 * @return 返回信息列表
	 * @throws Exception 异常
	 */
	public List<MShare> getShareListByUserName(
			String startIndex, String endIndex, String orderBy, String userName)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getShareListByUserName(con, startIndex, endIndex, orderBy, userName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

	/**
	 * 根据用户名获取分享信息列表总条数
	 * @param con数据库连接
	 * @param userName  用户名
	 * @return 返回信息列表总条数
	 * @throws Exception 异常
	 */
	public int getShareCountByUserName(String userName)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getShareCountByUserName(con, userName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 删除骑游趣事
	 * @param id 骑游趣事ID
	 * @return 返回删除影响的行数
	 * @throws Exception 异常
	 */
	public int deleteShareById(String id) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.deleteShareById(this.con, id);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
